/**
 *  登录模块
 *  @author zx
 *  @date 2019-11-01
 * */
$(function () {
    $('#kanbuq').click(function(){
        let ts = Date.parse(new Date())/1000;
        $('.captcha').attr('src', '/captcha.html?rand='+ts);
    });
    $("#loginInput").validate({
        rules:{
            username:{
                required:true,
                minlength:4,
                maxlength:18
            },
            password:{
                required:true,
                minlength:6,
                maxlength:18
            },
            captcha:{
                required:true,
                minlength:4,
                maxlength:6
            }
        },
        onkeyup:false,
        focusCleanup:true,
        success:"valid",
        submitHandler:function(form){
            $(form).ajaxSubmit({
                type: 'post',
                url: $(form).attr('action'),
                data: $(form).serialize(),
                success:function(data){
                    layer.msg('登陆成功.正在跳转...',{icon:1,time:1000});
                    setTimeout(function(){
                        window.location.href = '/admin'
                    }, 1000)
                },
                error:function(XmlHttpRequest, textStatus, errorThrown){
                    let errstr = XmlHttpRequest.responseText;
                    if ( errstr ) {
                        layer.msg($.parseJSON(errstr).message,{icon:5,time:1000});
                    } else {
                        layer.msg('服务错误!',{icon:5,time:1000});
                    }
                }
            });
            return false;
        }
    });
});